//
//  YKStepBtnView.m
//  MagicBox
//
//  Created by 胡文广 on 16/8/14.
//  Copyright © 2016年 ZiLiFang. All rights reserved.
//

#import "YKStepBtnView.h"

@interface YKStepBtnView ()
@property (nonatomic, strong, readwrite) NSMutableArray *labelAry;
@property (nonatomic, copy, readwrite) StepBtnClickedBlock currentBlock;
@property (nonatomic, strong, readwrite) UIView *lineView;
@property (nonatomic,strong) UILabel *selectLabel;
@end

@implementation YKStepBtnView

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    self.lineView = [UIView new];
    self.lineView.height = 2;
    self.lineView.backgroundColor = GLOBL_YELLOW;
    self.selectedIndex = 0;
    self.distanceBetween = 70;
    [self addSubview:self.lineView];
    return self;
}

+(YKStepBtnView *)stepBtnViewWithCenter:(CGPoint)center{
    YKStepBtnView *stepView = [[YKStepBtnView alloc]initWithFrame:CGRectZero];
    stepView.center = center;
    return stepView;

}

-(void)loadTitles:(NSArray *)titles withClickedBlock:(StepBtnClickedBlock)block{
    if (self.labelAry == nil) {
        self.labelAry = [@[]mutableCopy];
    }
    [self.labelAry makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.labelAry removeAllObjects];
    self.currentBlock = block;
    
    for (int i = 0; i < titles.count; i++) {
        
        UILabel *label = [UILabel new];
        label.backgroundColor = CLEAR;
        label.numberOfLines = 1;
//        label.font = [UIFont fontWithName:PFRegular size:17];
        label.text = titles[i];
//        label.textColor = BLACK;
        [label sizeToFit];
        label.width += 12;
        [self.labelAry addObject:label];
        label.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTapped:)];
        label.userInteractionEnabled = YES;
        [label addGestureRecognizer:tap];
        [self addSubview:label];
        label.textAlignment = NSTextAlignmentCenter;
        
        //添加红点
//        UIView *redPoint = [UIView new];
//        redPoint.width = 5;
//        redPoint.height = 5;
//        redPoint.backgroundColor = RED;
////        [redPoint cycloViewWithBorderWidth:0];
//        redPoint.bottom = label.top + 5;
//        redPoint.right = label.width;
//        [label addSubview:redPoint];
//        redPoint.tag = 1111;
        
        label.clipsToBounds = NO;
//        redPoint.hidden = YES;
    }
    [self layoutLabels];
}

-(void)labelTapped:(UITapGestureRecognizer *)tap{

    UILabel *label = (UILabel *)tap.view;
    NSInteger index = label.tag;
    self.selectedIndex = index;
    if (self.currentBlock) {
        self.currentBlock(index);
    }
}


- (void)updateSelLable:(NSInteger )index{

    for (int i = 0; i<self.labelAry.count; i++) {
        UILabel *label = self.labelAry[i];
        label.textColor = i == index?BLACK:GRAY;
    }
}
-(void)setDistanceBetween:(CGFloat)distanceBetween{

    _distanceBetween = distanceBetween;
    [self layoutLabels];
}

-(void)setSelectedIndex:(NSInteger)selectedIndex{
    
    _selectedIndex = selectedIndex;
    UILabel *label = self.labelAry[self.selectedIndex];
    self.lineView.width = label.width;
    self.lineView.bottom = self.height;
    [self updateSelLable:selectedIndex];
    [UIView  animateWithDuration:.2 animations:^{
        self.lineView.center = CGPointMake(label.center.x, self.lineView.center.y);
    }];
    
    
    
}

-(void)layoutLabels{
    
    if (self.labelAry.count == 0) {
        return;
    }
    float xoffset = 0;
    
    
    for (int i = 0; i < self.labelAry.count; i++) {
        UILabel *label = [self.labelAry objectAtIndex:i];
        label.left = xoffset;
        xoffset += label.width + self.distanceBetween;
        label.center = CGPointMake(label.center.x, 22);
        
    }
    self.width = xoffset - self.distanceBetween;
    self.height = 44;
    

    UILabel *label = self.labelAry[self.selectedIndex];
    self.lineView.width = label.width;
    self.lineView.bottom = self.height;
    self.lineView.center = CGPointMake(label.center.x, self.lineView.center.y);
}


-(void)show:(BOOL)show redPointAtIndex:(NSInteger)index{
    UILabel *label = [self.labelAry objectAtIndex:index];
    UIView *redPoint = [label viewWithTag:1111];
    redPoint.hidden = !show;
}


@end
